package com.smyh.model.host.state;

import android.content.Context;
import android.graphics.Color;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.smyh.UIM.host.state.GroupActivity;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.cache.UIM.host.GroupDeviceData;
import com.smyh.model.tableModel.KtableView;

import java.util.Map;

/**
 * Created by 姜龙 on 2016/7/8.
 */
public class GroupModel {

    private static TableLayout tableLayout;

    public static TableLayout getTableLayout(Context context){
        if(tableLayout == null){
            tableLayout = new TableLayout(context);
            tableLayout.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(null);
            String header0[] = {"模拟量"};
            String header1[] = {"","A相","B相","C相","0序"};
            String test1[] = {"电流","","","",""};
            String test2[] = {"电场","","","",""};
            String test3[] = {"电池电压","","","","--"};
            String test4[] = {"温度","","","","--"};
            String test5[] = {"指示器电容电压","","","","--"};
            String test6[] = {"故障电流","","","",""};
            String header2[] = {"遥信量"};
            String header3[] = {"","A相","B相","C相","0序","合并"};
            String test7[] = {"短路","","","","","--"};
            String test8[] = {"接地","","","","","--"};
            String test9[] = {"通讯","","","","","--"};
            String test10[] = {"过流","","","","","--"};
            String test11[] = {"速断","","","","",""};
            String test12[] = {"断电","","","","","--"};
            String test13[] = {"电池欠压","","","","","--"};
            String test14[] = {"上电","","","","","--"};
            String test15[] = {"心跳","","","","","--"};
            tableLayout.addView(KtableView.addTableHeaders(context,header0));
            tableLayout.addView(KtableView.addTableHeaders(context,header1));
            tableLayout.addView(KtableView.addNewRow(context,test1));
            tableLayout.addView(KtableView.addNewRow(context,test2));
            tableLayout.addView(KtableView.addNewRow(context,test3));
            tableLayout.addView(KtableView.addNewRow(context,test4));
            tableLayout.addView(KtableView.addNewRow(context,test5));
            tableLayout.addView(KtableView.addNewRow(context,test6));
            tableLayout.addView(KtableView.addTableHeaders(context,header2));
            tableLayout.addView(KtableView.addTableHeaders(context,header3));
            tableLayout.addView(KtableView.addNewRow(context,test7));
            tableLayout.addView(KtableView.addNewRow(context,test8));
            tableLayout.addView(KtableView.addNewRow(context,test9));
            tableLayout.addView(KtableView.addNewRow(context,test10));
            tableLayout.addView(KtableView.addNewRow(context,test11));
            tableLayout.addView(KtableView.addNewRow(context,test12));
            tableLayout.addView(KtableView.addNewRow(context,test13));
            tableLayout.addView(KtableView.addNewRow(context,test14));
            tableLayout.addView(KtableView.addNewRow(context,test15));
        }
        return tableLayout;
    }

    public static void setTableLayout() {
        Map<Integer,Integer> dM = null ,mM= null;
        Map<Integer,Map<Integer,Integer>> map1 =  GroupDeviceData.digitMap.get(MainCahce.chooseDeviceAddr);
        if(map1 != null){
            dM =  map1.get(GroupActivity.groupNum);
        }
        Map<Integer,Map<Integer,Integer>> map2 = GroupDeviceData.mockMap.get(MainCahce.chooseDeviceAddr);
        if(map2 != null){
            mM =  map2.get(GroupActivity.groupNum);
        }

        for(int i=2;i<tableLayout.getChildCount();i++){
            TableRow childRow = (TableRow) tableLayout.getChildAt(i);
            switch (i){
                case 2 ://电流（ABC0）
                    if (mM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(mM.get(1)==null?"":mM.get(1)+"");
                        ((TextView) childRow.getChildAt(2)).setText(mM.get(2)==null?"":mM.get(2)+"");
                        ((TextView) childRow.getChildAt(3)).setText(mM.get(3)==null?"":mM.get(3)+"");
                        ((TextView) childRow.getChildAt(4)).setText(mM.get(4)==null?"":mM.get(4)+"");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                    }
                    break;
                case 3 ://电场（ABC0）
                    if (mM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(mM.get(5)==null?"":mM.get(5)+"");
                        ((TextView) childRow.getChildAt(2)).setText(mM.get(6)==null?"":mM.get(6)+"");
                        ((TextView) childRow.getChildAt(3)).setText(mM.get(7)==null?"":mM.get(7)+"");
                        ((TextView) childRow.getChildAt(4)).setText(mM.get(8)==null?"":mM.get(8)+"");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                    }
                    break;
                case 4 ://电池电压
                    if (mM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(mM.get(9)==null?"":mM.get(9)+"");
                        ((TextView) childRow.getChildAt(2)).setText(mM.get(10)==null?"":mM.get(10)+"");
                        ((TextView) childRow.getChildAt(3)).setText(mM.get(11)==null?"":mM.get(11)+"");
                        ((TextView) childRow.getChildAt(4)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText("--");
                    }
                    break;
                case 5 ://温度
                    if (mM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(mM.get(12)==null?"":mM.get(12)+"");
                        ((TextView) childRow.getChildAt(2)).setText(mM.get(13)==null?"":mM.get(13)+"");
                        ((TextView) childRow.getChildAt(3)).setText(mM.get(14)==null?"":mM.get(14)+"");
                        ((TextView) childRow.getChildAt(4)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText("--");
                    }
                    break;
                case 6 ://指示器电容电压
                    if (mM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(mM.get(15)==null?"":mM.get(15)+"");
                        ((TextView) childRow.getChildAt(2)).setText(mM.get(16)==null?"":mM.get(16)+"");
                        ((TextView) childRow.getChildAt(3)).setText(mM.get(17)==null?"":mM.get(17)+"");
                        ((TextView) childRow.getChildAt(4)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText("--");
                    }
                    break;
                case 7 ://故障电流
                    if (mM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(mM.get(18)==null?"":mM.get(18)+"");
                        ((TextView) childRow.getChildAt(2)).setText(mM.get(19)==null?"":mM.get(19)+"");
                        ((TextView) childRow.getChildAt(3)).setText(mM.get(20)==null?"":mM.get(20)+"");
                        ((TextView) childRow.getChildAt(4)).setText(mM.get(21)==null?"":mM.get(21)+"");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                    }
                    break;
                case 10 ://短路
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(1)==null?"":dM.get(1)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(2)==null?"":dM.get(2)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(3)==null?"":dM.get(3)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(4)==null?"":dM.get(4)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
                case 11 ://接地
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(5)==null?"":dM.get(5)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(6)==null?"":dM.get(6)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(7)==null?"":dM.get(7)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(8)==null?"":dM.get(8)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
                case 12 ://通讯
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(9)==null?"":dM.get(9)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(10)==null?"":dM.get(10)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(11)==null?"":dM.get(11)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(12)==null?"":dM.get(12)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
                case 13 ://过流
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(13)==null?"":dM.get(13)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(14)==null?"":dM.get(14)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(15)==null?"":dM.get(15)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(16)==null?"":dM.get(16)+"");
                        ((TextView) childRow.getChildAt(5)).setText(dM.get(17)==null?"":dM.get(17)+"");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText(" ");
                    }
                    break;
                case 14 ://速断
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(18)==null?"":dM.get(18)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(19)==null?"":dM.get(19)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(20)==null?"":dM.get(20)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(21)==null?"":dM.get(21)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
                case 15 ://断电
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(22)==null?"":dM.get(22)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(23)==null?"":dM.get(23)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(24)==null?"":dM.get(24)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(25)==null?"":dM.get(25)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
                case 16 ://电池欠压
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(26)==null?"":dM.get(26)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(27)==null?"":dM.get(27)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(28)==null?"":dM.get(28)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(29)==null?"":dM.get(29)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
                case 17 ://上电
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(30)==null?"":dM.get(30)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(31)==null?"":dM.get(31)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(32)==null?"":dM.get(32)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(33)==null?"":dM.get(33)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
                case 18 ://心跳
                    if (dM!=null){
                        ((TextView) childRow.getChildAt(1)).setText(dM.get(34)==null?"":dM.get(34)+"");
                        ((TextView) childRow.getChildAt(2)).setText(dM.get(35)==null?"":dM.get(35)+"");
                        ((TextView) childRow.getChildAt(3)).setText(dM.get(36)==null?"":dM.get(36)+"");
                        ((TextView) childRow.getChildAt(4)).setText(dM.get(37)==null?"":dM.get(37)+"");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }else{
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        ((TextView) childRow.getChildAt(2)).setText(" ");
                        ((TextView) childRow.getChildAt(3)).setText(" ");
                        ((TextView) childRow.getChildAt(4)).setText(" ");
                        ((TextView) childRow.getChildAt(5)).setText("--");
                    }
                    break;
            }
        }
    }
}
